package com.example.onlinemonitoring.comCenter;
import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Tgsensor;
import org.springframework.context.annotation.Scope;
import com.example.onlinemonitoring.serviceImpl.TgsensorServiceImpl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service("tg61850Com")
@Scope("prototype")
public class Tg61850Com extends CommPortManager  {
    @Autowired
    private TgsensorServiceImpl tgserviceimpl;
    @Override
    public void receiveData(String str) {  
        setText("收到sanway请求：" + str + "\r\n");       
        int addr = parseHex4(str.substring(8, 12));                                                                                    
        String s = "";
        Tgsensor tg = new Tgsensor();
        tg.setStationid(ad.getStationid());
        tg.setDevicegroup(ad.getDevicegroup());
        tg.setDeviceaddr(addr);
        tg = tgserviceimpl.getModel(tg);
        if(tg == null)
        tg = new Tgsensor();
        s += "FF5550A6";
        s += str.substring(8, 12);
        s += "00000000";//设备健康状态
        s += "00000000";//A电压
        s += "00000000";//B电压
        s += "00000000";//C电压
        s += "00000000";//参考相角
        s += "00000000";//参考电容
        s += "00000000";//参考功率
        s += "00000000";//投运参考电压
        s += IEEE754Utils.singleToHex(isnull(tg.getLastawcurrent()));//A相电流
        s += IEEE754Utils.singleToHex(isnull(tg.getLastbwcurrent()));//B相电流
        s += IEEE754Utils.singleToHex(isnull(tg.getLastcwcurrent()));//C相电流
        s += "00000000";//A相电容
        s += "00000000";//B相电容
        s += "00000000";//C相电容
        s += IEEE754Utils.singleToHex(isnull(tg.getLasttandeltaa()));//A介损
        s += IEEE754Utils.singleToHex(isnull(tg.getLasttandeltab()));//B介损
        s += IEEE754Utils.singleToHex(isnull(tg.getLasttandeltac()));//C介损
        s += IEEE754Utils.singleToHex((new Date().getTime()-starttime)/1000f);//A运行时间
        s += IEEE754Utils.singleToHex((new Date().getTime()-starttime)/1000f);//B运行时间
        s += IEEE754Utils.singleToHex((new Date().getTime()-starttime)/1000f);//C运行时间
        s += CRC16.calcCrc16(s);
        Send(s);
        setText("SANWAY模块转发套管数据：" + s + "\r\n");
    }
}
